﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections.ObjectModel;
using MsSpatialTools;
using SharpMap;
using SharpMap.Geometries;
using SharpMap.Forms;
using SharpMap.Data.Providers;
using SharpMap.Layers;
namespace QueryVisualizerTools
{
    public partial class QueryFrm : Form
    {
        public QueryFrm()
        {
            InitializeComponent();
        }

        private void QueryFrm_Load(object sender, EventArgs e)
        {
            bindDB();
        }
        /// <summary>
        /// 绑定数据库名
        /// </summary>
        private void bindDB()
        { 
            DataTable dt = DBHelper.getSQLSchemaDB();
            this.tsCbbDB.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                this.tsCbbDB.Items.Add(row["name"].ToString());
            }
            //this.tsCbbDB.SelectedIndex = 0;
        }
        private void tsCbbDB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tsCbbDB.Text != "")
            {
                DataTable dt = DBHelper.getSQLSchemaTable(this.tsCbbDB.Text);
                this.tsCbbTB.Items.Clear();
                foreach (DataRow row in dt.Rows)
                {
                    this.tsCbbTB.Items.Add(row["name"].ToString());
                }
                //this.tsCbbTB.SelectedIndex = 0;
            }
            this.rtxtSQL.Text = "";
        }
        private void tsCbbTB_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DBHelper.getSQLSchemaTable(this
            this.doBuildSQL(this.tsCbbTB.Text);
        }
        private void doBuildSQL(string tbName)
        {
            //DataTable dt = DBHelper.getSQLColumnName(tbName);
            DBHelper.Database = this.tsCbbDB.Text;
            string geoName = DBHelper.getSQLGeoName(tbName);
            string sqlTxt = "select *";
            if (geoName != "")
                sqlTxt += ",st.astext(" + geoName + ") as geometry";
            sqlTxt += " from " + tbName;
            this.rtxtSQL.Text += sqlTxt + ";\n";
        }
        private void 运行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            run();
        }
        private void tsbtnRun_Click(object sender, EventArgs e)
        {
            run();
        }

        private void run()
        {
            string mysql = "";
            foreach (Control c in this.tcSqlTxt.SelectedTab.Controls)
            {
                if (c.GetType().Name == "RichTextBox")
                {
                    RichTextBox rtb = (RichTextBox)c;
                    mysql = rtb.Text;
                }
            }
            this.doQuery(mysql);
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        private void doQuery(string mysql)
        {
            try
            {
                MsSqlHelper mss = new MsSqlHelper(this.tsCbbDB.Text);
                //LayerCollection lc= mss.getLayerCollection(mysql);
                //mss.addToMapControl(lc, this.mapControl);

                DataSet ds = mss.getDataSet(mysql);
                mss.addToMapControl(ds, this.mapControl);

                TabHelper th = new TabHelper();
                th.addToTab(ds, this.tcDisplay);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }
        private void 改变地图底色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cg = new ColorDialog();
            if (cg.ShowDialog() == DialogResult.OK)
            {
                this.mapControl.Map.BackColor = cg.Color;          
                this.mapControl.Refresh();
            } 
        }

        private void tsPan_Click(object sender, EventArgs e)
        {
            changeCheck(this.tsPan);
            changeMapTool(MapImage.Tools.Pan);  
            //tsPan
        }

        private void tsZoomIn_Click(object sender, EventArgs e)
        {
            changeCheck(this.tsZoomIn);
            changeMapTool(MapImage.Tools.ZoomIn);  
        }

        private void tsZoomOut_Click(object sender, EventArgs e)
        {
            changeCheck(this.tsZoomOut);
            changeMapTool(MapImage.Tools.ZoomOut);
            
        }

        private void tsNone_Click(object sender, EventArgs e)
        {
            changeCheck(this.tsNone);
            changeMapTool(MapImage.Tools.None);  
            //this.mapControl.mou
        }

        private void tsZoomFull_Click(object sender, EventArgs e)
        {
            changeCheck(this.tsZoomFull);
            changeMapTool();
        }

        private void changeMapTool( MapImage.Tools mim)
        {
            if (this.mapControl.Map.Layers.Count>0)
            {
                this.mapControl.ActiveTool = mim;
            }
        }
        private void changeMapTool()
        {
            if (this.mapControl.Map.Layers.Count > 0)
            {
                this.mapControl.Map.ZoomToExtents();
                this.mapControl.Refresh();
            }
        }
        private void changeCheck(ToolStripButton tsb)
        {
            //this.tsPan.Checked
            foreach (ToolStripItem tsi in toolStrip2.Items)
            {
                if (tsi.GetType().Name == "ToolStripButton")
                {
                    ToolStripButton tmp = (ToolStripButton)tsi;
                    tmp.Checked = tmp.Equals(tsb);                
                }
            }
            
        }
        private void tsClear_Click(object sender, EventArgs e)
        {
            this.mapControl.Map.Layers.Clear(); 
            this.mapControl.Refresh();
        }

        private void tsQuery_Click(object sender, EventArgs e)
        {
            changeMapTool(MapImage.Tools.Query);  
        }

        private void QueryFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
            this.Dispose();            
        }
    }
}
